package com.ellricky.baselib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.andr.common.ui.adapter.BaseViewPagerAdapter;
import com.andr.common.widget.NoScrollViewPager;
import com.andr.common.widget.bnb.BnbUtil;
import com.andr.common.widget.bnb.OnBnbListener;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ellricky.baselib.R;

import java.util.Iterator;
import java.util.List;

public class NewAppMainView extends LinearLayout implements BottomNavigationBar.OnTabSelectedListener {
    private NoScrollViewPager vp;
    private BottomNavigationBar bnb;
    private OnBnbListener onBnbListener;
    private int curPos = 0;

    public NewAppMainView(Context context) {
        super(context);
        this.init(context);
    }

    public NewAppMainView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public NewAppMainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    private void init(Context context) {
        View.inflate(context, R.layout.common_app_main_view, this);
        this.vp = this.findViewById(R.id.vpMain);
        this.bnb = this.findViewById(R.id.bnbBottom);
        this.vp.setOffscreenPageLimit(4);

    }

    public void setOnBnbListener(OnBnbListener onBnbListener) {
        this.onBnbListener = onBnbListener;
    }

    public void initBnb(List<BottomNavigationItem> items) {
        Iterator var2 = items.iterator();

        while (var2.hasNext()) {
            BottomNavigationItem item = (BottomNavigationItem) var2.next();
            this.bnb.addItem(item);
        }

        this.bnb.initialise();
        BnbUtil.setIconItemMargin(this.getContext(), this.bnb, 5, 12);
        this.bnb.setTabSelectedListener(this);
    }

    public void initVP(List<Fragment> fragments, FragmentManager fm) {
        if (fragments.size() > 0) {
            BaseViewPagerAdapter adapter = new BaseViewPagerAdapter(fm, fragments);
            this.vp.setAdapter(adapter);
        }

    }

    public void onTabSelected(int position) {
        boolean canJump = true;
        if (this.onBnbListener != null) {
            canJump = this.onBnbListener.onSelectTab(position);
        }

        if (canJump) {
            this.curPos = position;
            this.vp.setCurrentItem(position,false);
        } else {
            this.bnb.selectTab(this.curPos);
        }

    }

    public void onTabUnselected(int position) {
    }

    public void onTabReselected(int position) {
    }

    public void setTab(int position) {
        this.vp.setCurrentItem(position, false);
        this.bnb.selectTab(position);
    }
}
